from db_utils import *
from menu_ext import *
from matplotlibgaoyang import *


def display_menu():
    title = "\n===== 学生信息管理系统 ====="
    print("\n" + title.center(40))
    print("1. 添加学生")
    print("2. 查看所有学生")
    print("3. 搜素学生")
    print("4. 修改学生成绩")
    print("5. 删除学生")
    print("6. 批量随机添加学生")
    print("7. 导出学生成绩到excel")
    print("8. 按照班级平均分排序")
    print("9. 生成班级平均分三维柱状图")
    print("0. 退出")


def add_student():
    name = input("请输入学生姓名: ")
    gender = input("请输入性别 (男/女) : ")
    age = int(input("请输入年龄: "))
    student_id = input("请输入学号: ")
    cs = input("请输入班级 (6年级1班到6班): ")
    chinese_score = input("请输入语文成绩 (60-100,保留2位小数点): ")
    math_score = input("请输入数学成绩 (60-100,保留2位小数点): ")
    english_score = input("请输入英语成绩 (60-100,保留2位小数点): ")
    insert_student((name, gender, age, student_id, cs, chinese_score, math_score, english_score))


def show_all_students():
    students = get_all_students()
    for s in students:
        print(s)


def search_student_by_name():
    name = input("请输入要搜素的学生姓名: ")
    results = search_student(name)
    if results:
        for s in results:
            print(s)
    else:
        print("未找到相关学生")


def update_student_score():
    student_id = input("请输入要修改成绩的学生学号: ")
    print("请选择要更新的成绩科目: ")
    print("1. 语文")
    print("2. 数学")
    print("3. 英语")
    choice = input("请输入选项 (1/2/3): ")

    if choice == "1":
        new_score = float(input("请输入新的语文成绩: "))
        subject_field = "chinese_score"
    elif choice == "2":
        new_score = float(input("请输入新的数学成绩: "))
        subject_field = "math_score"
    elif choice == "3":
        new_score = float(input("请输入新的英语成绩: "))
        subject_field = "english_score"
    else:
        print("无效的选择! ")
        return
    update_student(student_id, subject_field, new_score)


def delete_student_info():
    student_id = input("请输入要删除的学生学号: ")
    delete_student(student_id)


def main_loop():
    create_database()
    create_table()
    while True:
        display_menu()
        choice = input("请选择操作: ")

        if choice == "1":
            add_student()
        elif choice == "2":
            show_all_students()
        elif choice == "3":
            search_student_by_name()
        elif choice == "4":
            update_student_score()
        elif choice == "5":
            if login_for_delete():
                delete_student_info()
            else:
                print("删除失败, 权限不足")
        elif choice == "6":
            bulk_add_students()
        elif choice == "7":
            export_to_excel()
        elif choice == "8":
            get_class_averages()
        elif choice == "9":
            matplotlib3()
        elif choice == "0":
            print("再见")
            break
        else:
            print("输入无效，请重新选择")